<?php
/**
 * Created by PhpStorm.
 * User: 86151
 * Date: 2024/3/6
 * Time: 14:44
 */

namespace app\admin\model;
use think\Config;
use think\Db;
use think\Model;

class Auth extends Model
{
    static public function userInfo($user_id){
        $user = Db::name('user')->where('id',$user_id)->find();
        if(!$user['gzh_open_id']){
            exit(json_encode(array('code' =>401, 'msg' =>'未授权,请重新登录'), JSON_UNESCAPED_UNICODE));
        }
        $user_qy= Db::name('user_qy')->where('vip_id','in',Config::get('vip_info'))->where('end_time','>=',date('Y-m-d H:i:s',time()))->where('mobile',$user['mobile'])->find();
        if(!$user_qy){
            exit(json_encode(array('code' =>0, 'msg' =>'未绑定号码,请重新登录'), JSON_UNESCAPED_UNICODE));
        }
        return $user;
    }


    //卡密核销码
    static public function kmjx($order,$v){
        $effective_time = substr($v['effective_time'], 0, 10);
        if (isset($v['goods_number'])) {
            $data = Orangs::decrypt($v['goods_number']);
            $kh = $data;
        } else {
            $kh = '';
        }
        if (isset($v['goods_password'])) {
            $data = Orangs::decrypt($v['goods_password']);
            $jm = $data;
        } else {
            $jm = '';
        }
        if (isset($v['goods_link'])) {
            $data = Orangs::decrypt($v['goods_link']);
            $lj = $data;
        } else {
            $lj = '';
        }
        Db::name('voucher')->insert([
            'item_id' => self::makeOrderNum(),
            'voucher_id' => $order['resource_id'],
            'effect_time' => $order['createtime'],
            'expire_time' => date('Y-m-d H:i:s', $effective_time),
            'title' => $order['title'],
            'ticket_url' => $lj,
            'kh' => $kh,
            'ticket_code' => $jm,
            'type' => 5,
            'user_id' => $order['user_id'],
            'order_id' => $order['id'],
            'order_amount' => $order['amount'],
            'voucher_status'=>'EFFECTIVE'
        ]);
//        Db::name('order')->where('id',$order['id'])->update(['status'=>4]);
    }
    public static function makeOrderNum()
    {
        @date_default_timezone_set("PRC");
        while (true) {
            $order_id_main = date('YmdHis') . rand(10000, 99999);//5位数
            $order_id_len = strlen($order_id_main);
            $order_id_sum = 0;
            for ($i = 0; $i < $order_id_len; $i++) {
                $order_id_sum += (int)(substr($order_id_main, $i, 1));
            }
            $order_id = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100, 2, '0', STR_PAD_LEFT);
            return $order_id;

        }
    }
}